<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        function ListNode(val, next) {
            this.val = (val === undefined ? 0 : val)
            this.next = (next === undefined ? null : next)
        }
        // https://leetcode-cn.com/problems/remove-linked-list-elements/
        // 这是标准写法好像，就按照这么写，返回的不是head，要返回的是新增的头节点的next
        var removeElements = function(head, val) {
            let header = new ListNode(0, null);
            header.next = head;
            let cur = header;
            while (cur.next != null) {
                if (cur.next.val === val) {
                    cur.next = cur.next.next;
                } else {
                    cur = cur.next;
                }
            }
            return header.next;
        };
        
        var removeElements2 = function(head, val) {
            // 删除头节点
            while(head != null && head.val === val) {
                head = head.next;
            }

            // 删除非头节点
            let cur = head;
            while(cur != null && cur.next != null) {
                if(cur.next.val == val) {
                    cur.next = cur.next.next;
                } else {
                    cur = cur.next;
                }
            }

            return head;
        };
        
        
        
        let node1 = new ListNode(7, null);
        let node2 = new ListNode(7, null);
        let node3 = new ListNode(7, null);
        let node4 = new ListNode(7, null);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        removeElements(node1, 7);
    </script>
</body>
</html>